<script setup lang="ts">
import { ref, computed, watch, type Ref } from "vue";
import { useAppStoreHook } from "@/store/modules/app";
import {
  delay,
  useDark,
  useECharts,
  type EchartOptions
} from "@pureadmin/utils";
import * as echarts from "echarts";

const { isDark } = useDark();

const theme: EchartOptions["theme"] = computed(() => {
  return isDark.value ? "dark" : "light";
});

const barChartRef = ref<HTMLDivElement | null>(null);
const { setOptions, resize } = useECharts(barChartRef as Ref<HTMLDivElement>, {
  theme
});

setOptions(
  {
    legend: {
      data: [
        { name: "GitHub信息", icon: "circle" },
        { name: "222", icon: "circle" }
      ]
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    grid: {
      containLabel: true,
      top: "30px",
      bottom: "0",
      left: "0",
      right: "0"
    },
    xAxis: [
      {
        type: "category",
        axisTick: {
          alignWithLabel: true
        },
        axisLabel: {
          interval: 0
          // width: "70",
          // overflow: "truncate"
        },
        data: ["4.11", "4.11", "4.11", "4.11", "4.11", "4.11", "4.11", "4.11"],
        triggerEvent: true
      }
    ],
    yAxis: [
      {
        type: "value",
        triggerEvent: true
      }
    ],
    series: [
      {
        name: "GitHub信息",
        type: "bar",
        data: [10, 100, 20, 66, 10, 100, 20, 66],
        barWidth: 12,
        itemStyle: {
          borderRadius: [6, 6, 6, 6],
          color: new echarts.graphic.LinearGradient(1, 1, 1, 0, [
            {
              offset: 0,
              color: "#85a0fd"
            },
            {
              offset: 0.5,
              color: "#5e81f0"
            },
            {
              offset: 1,
              color: "#426ce7"
            }
          ])
        }
      },
      {
        name: "222",
        type: "bar",
        data: [10, 100, 20, 66, 10, 100, 20, 66],
        barWidth: 12,
        itemStyle: {
          borderRadius: [6, 6, 6, 6],
          color: new echarts.graphic.LinearGradient(1, 1, 1, 0, [
            {
              offset: 0,
              color: "#f2b478"
            },
            {
              offset: 0.5,
              color: "#e9a34d"
            },
            {
              offset: 1,
              color: "#e39933"
            }
          ])
        }
      }
    ],
    addTooltip: true
  },
  {
    name: "click",
    callback: params => {
      console.log("click", params);
    }
  }
);

watch(
  () => useAppStoreHook().getSidebarStatus,
  () => {
    delay(600).then(() => resize());
  }
);
</script>

<template>
  <div ref="barChartRef" style="width: 100%; height: 100%" />
</template>
